package com.sniper.web.sitemesh;

import com.sniper.config.Global;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

import java.util.ArrayList;
import java.util.List;

public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {

    public String getDefaultTemplate() {
        return Global.getString("defaultTemplate");
    }

    public String getAdminPath() {
        return Global.getString("adminPath");
    }

    private String template = "/WEB-INF/modules/themes/";

    /**
     * 动态设置取消模板地址
     */
    public static List<String> excludePath = new ArrayList<>();

    static {
        excludePath.add("/topic*");
        excludePath.add("/Assistants*");
        excludePath.add("/myfiles*");
        excludePath.add("/search*");
        excludePath.add("/api*");
        excludePath.add("/echarts*");
        //首页
        excludePath.add("/index.html");
    }


    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {


        //不包含错误页面
        builder.setIncludeErrorPages(false);

        builder
                //后台
                .addDecoratorPath(getAdminPath() + "*", template + getDefaultTemplate() + "/admin/admin.jsp")
                .addExcludedPath(getAdminPath() + "/login*")
                .addExcludedPath(getAdminPath() + "/admin-print*")
                .addExcludedPath(getAdminPath() + "/file-upload*")
                .addExcludedPath(getAdminPath() + "/os*")
                .addExcludedPath("password*")
                //前台
                .addExcludedPath("/upload*");
        //.addExcludedPath("/oauth2/*")
        //.addExcludedPath("/error/*")
        //专题
        //d动态处理
        for (String _s : excludePath) {
            builder.addExcludedPath(_s);
        }

        builder.addDecoratorPath("/*", template + "home/home.jsp");
        //builder.addDecoratorPath("/", template + "home/index-cache.jsp");
        //.addDecoratorPath("/index", template + "home/index-cache.jsp")
        //.addExcludedPath("/index.html")

    }
}
